// Created by Sun on 12/4/2015.
package com.yellowpage.tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;

import com.yellowpage.tool.messageutil.AESUtils;
import com.yellowpage.tool.messageutil.RequestSms;
import com.yellowpage.tool.messageutil.UTF8PostMethod;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import com.alibaba.fastjson.JSON;


public class Message {

    private static InputStream is;

    public static void main(String args[]) throws Exception
    {
        //设置收件人
        String content = "日期: 20151210\n" +
                "ios活跃用户数: 337\n" +
                "Android活跃用户数: 42.44万 \n" +
                "主动用户数: 40.27万 \n" +
                "被动用户数: 31.78万 \n" +
                "累计用户数: 350.16万 \n" +
                "当日用户数: 42.47万 \n" +
                "新增用户数: 2.28万 \n";

        groupSendMessage(content,1);
    }

    //做一个群发的模块
    public static void groupSendMessage(String res,int type) throws Exception
    {
        //设置收件人
        List<String> userList = UserGet.getUserList(type,2);

        //转为UTF-8
        //String iso = new String(res.getBytes("UTF-8"),"ISO-8859-1");
        //String content = new String(iso.getBytes("ISO-8859-1"),"UTF-8");
//        byte[] tempstring = res.getBytes("GBK");
//        String content = new String(tempstring,"UTF-8");
        //给每个收件人发送短信
        for (Iterator<String> iter = userList.iterator();iter.hasNext();)
        {
            String temp = iter.next();
            if (!temp.equals(""))
                testApply(res,temp);
        }

    }

    public static void testApply(String content,String telenum) throws Exception {
        //HttpClient
        HttpClient httpClient = new HttpClient();

        String url = "http://101.227.244.95:9080/hy114/SmsSend";
        PostMethod postMethod = new UTF8PostMethod(url);

        //RequestSmsGet
        RequestSms request = new RequestSms();
        request.setChannelno("监控");
        request.setQueryNum(telenum);
        String res = new String(content.getBytes("UTF-8"),"UTF-8");

        request.setContent(res);
        String info=JSON.toJSONString(request);
        System.out.println("info="+info);
        info = AESUtils.encrypt4AES(info, "APP&AES@");
        postMethod.setParameter("info", info);
        System.out.println("info=" + info);
        // 执行postMethod
        try {

            //发送短信
            int statusCode = httpClient.executeMethod(postMethod);
            // HttpClient对于要求接受后继服务的请求，像POST和PUT等不能自动处理转发
            // 301或302
            System.out.println("statusCode----->>" + statusCode);
            if (statusCode == HttpStatus.SC_OK) {
                System.out.println("httpclient return ok----->");
                //获取返回数据
                is = postMethod.getResponseBodyAsStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                StringBuffer tempString = new StringBuffer();
                String str=null;
                while((str = reader.readLine()) != null) {
                    tempString.append(str);
                }
            }
        } catch (HttpException e) {
            //发生致命的异常，可能是协议不对或者返回的内容有问�?
            System.out.println("Please check your provided http address!");
            e.printStackTrace();
        } catch (IOException e) {
            //发生网络异常
            e.printStackTrace();
        } finally {
            //释放连接
            postMethod.releaseConnection();
        }
    }

}
